//
//  SendMailViewController.m
//  PlayerFix
//
//  Created by DAT on 15/11/10.
//  Copyright © 2015年 DAT. All rights reserved.
//

#import "SendMailViewController.h"
#import <MessageUI/MessageUI.h>

@interface SendMailViewController ()<MFMailComposeViewControllerDelegate>

@property (nonatomic, strong) UIView *naView;
@property (nonatomic, strong) UILabel *naTitle;
@property (nonatomic, strong) UITableView *bgTableView;


@end

@implementation SendMailViewController

//- (void)viewWillAppear:(BOOL)animated{
//    
//    self.naView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
//    self.naView.backgroundColor = [UIColor colorWithRed:30 / 255.0 green:80 / 255.0 blue:100 / 255.0 alpha:1];
//    [self.view addSubview:self.naView];
//    
//    self.naTitle = [[UILabel alloc] initWithFrame:CGRectMake((375 - 100) / 2, 25, 100, 30)];
//    self.naTitle.backgroundColor = [UIColor clearColor];
//    self.naTitle.textColor = [UIColor whiteColor];
//    self.naTitle.textAlignment = NSTextAlignmentCenter;
//    self.naTitle.font = [UIFont boldSystemFontOfSize:18];
//    self.naTitle.text = @"意见反馈";
//    [self.naView addSubview:self.naTitle];
//    
//    UIButton *goBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    goBackBtn.frame = CGRectMake(10, 25, 30, 30);
//    goBackBtn.backgroundColor = [UIColor clearColor];
//    [goBackBtn setBackgroundImage:[UIImage imageNamed:@"backBtn.png"] forState:UIControlStateNormal];
//    [goBackBtn addTarget:self action:@selector(goBackAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.naView addSubview:goBackBtn];
//
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.naView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    self.naView.backgroundColor = [UIColor colorWithRed:30 / 255.0 green:80 / 255.0 blue:100 / 255.0 alpha:1];
    [self.view addSubview:self.naView];
    
    self.naTitle = [[UILabel alloc] initWithFrame:CGRectMake((375 - 100) / 2, 25, 100, 30)];
    self.naTitle.backgroundColor = [UIColor clearColor];
    self.naTitle.textColor = [UIColor whiteColor];
    self.naTitle.textAlignment = NSTextAlignmentCenter;
    self.naTitle.font = [UIFont boldSystemFontOfSize:18];
    self.naTitle.text = @"意见反馈";
    [self.naView addSubview:self.naTitle];
    
    UIButton *goBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackBtn.frame = CGRectMake(10, 25, 30, 30);
    goBackBtn.backgroundColor = [UIColor clearColor];
    [goBackBtn setBackgroundImage:[UIImage imageNamed:@"backBtn.png"] forState:UIControlStateNormal];
    [goBackBtn addTarget:self action:@selector(goBackAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.naView addSubview:goBackBtn];


    [self sendEmail];
    
    
}

#pragma -- 发送邮件
- (void)sendEmail {
    
    // 查找当前工程有没有字符串中的这个类 / 邮件视图控制器
    Class mailClass = NSClassFromString(@"MFMailComposeViewController");
    if (mailClass != nil) {
        // 判断当前设备是否设置 邮件账户
        if ([mailClass canSendMail]) {
            
            [self displayMailBox];
        }
        else {
            
            [self showMailBoxOnDevice];
        }
    }
    else {
        [self showMailBoxOnDevice];
    }
}

#pragma -- 创建并模态进入编辑视图
- (void)displayMailBox {
    // 创建邮件视图
    MFMailComposeViewController *mailBox = [[MFMailComposeViewController alloc] init];
    // 添加收件地址
    NSArray *recipient = [NSArray arrayWithObject:@"shangrjtest@sina.com"];
    [mailBox setToRecipients:recipient];
    // 填写主题
    [mailBox setSubject:@"意见反馈"];
    // 邮件内容
    NSString *messageBody = @"app做的太好了";
    [mailBox setMessageBody:messageBody isHTML:YES];
    // 添加文件
    //    UIImage *attachedPic = [UIImage imageNamed:@"hehe.jpg"];
    //    NSData *data = UIImagePNGRepresentation(attachedPic);
    //    [mailBox addAttachmentData:data mimeType:@"" fileName:@"13.jpg"];
    // 设置代理人
    mailBox.mailComposeDelegate = self;
    
    [self presentViewController:mailBox animated:YES completion:^{
        
    }];
}

#pragma -- iOS自带邮箱
- (void)showMailBoxOnDevice {
    NSString *mail = @"mailto:shangrjtest@sina.com";
    mail = [mail stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mail]];
}

// 协议方法
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    NSString *message;
    switch (result) {
        case MFMailComposeResultCancelled:
            message = @"取消发送";
            NSLog(@"%@", message);
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];

            break;
        case MFMailComposeResultSaved:
            message = @"保存草稿";
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];

            NSLog(@"%@", message);
            break;
        case MFMailComposeResultSent:
            message = @"已发送";
            NSLog(@"%@", message);
            [self sendResult];
            break;
        case MFMailComposeResultFailed:
            message = @"发送失败";
            NSLog(@"%@ 错误信息:%@", message, error);
            break;
        default :
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
    }
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    //[self.navigationController popToRootViewControllerAnimated:NO];
}

- (void) sendResult {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"感谢您的反馈" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    

    [alert show];
}

- (void)goBackAction:(UIButton *)btn{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
